importClass(android.view.WindowManager);
importClass("android.view.Gravity");
importClass("android.graphics.PixelFormat");

let x = 0;
let y = 0;

let window1 = floaty.window(
  <frame id="action" w="{{device.width}}" h="{{device.height}}">
  </frame>
);
window1.setSize(-1, -1);
let actionParent = window1.action.parent.parent.parent;

setTouchable(actionParent, true);

setInterval(() => {
  if (!!x && !!y) {
    click(x, y);
  }
}, 80);

function setTouchable(view, touchable) {
  let params = view.getLayoutParams();
  if (touchable) {
    params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
  } else {
    params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
  }
  //log(params.flags);
  windowManager = context.getSystemService(context.WINDOW_SERVICE);
  ui.run(function () {
    windowManager.updateViewLayout(view, params);
  })
}

window1.action.setOnTouchListener(function (view, event) {
  switch (event.getAction()) {
    case event.ACTION_DOWN:
      x = event.getRawX();
      y = event.getRawY();
      return true;
    case event.ACTION_MOVE:
      return true;
    case event.ACTION_UP:
      return true;
  }
  return true;
});

let window2 = floaty.rawWindow(
  <frame id="parent" gravity="cneter" bg="#f0f0f0">
    <button id="btn" textSize="18sp"></button>
  </frame>
);
ui.run(function () {
  window2.setSize(200, 200);
  window2.setPosition(1180, 400);
  //window2.setTouchable(true);
  window2.btn.setText("关");
});

window2.btn.click(function () {
  let params = actionParent.getLayoutParams();
  if (params.flags == 16777736) {
    //console.log("当前可以触摸, 改为不可以触摸");
    ui.run(function () { window2.btn.setText("开"); });
    params.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
  } else if (params.flags == 16777752) {
    //console.log("当前不可以触摸, 改为可以触摸");
    ui.run(function () { window2.btn.setText("关"); });
    params.flags &= ~WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    // params.width = WindowManager.LayoutParams.MATCH_PARENT;
    // params.height = WindowManager.LayoutParams.MATCH_PARENT;
  } else {

  }
  windowManager = context.getSystemService(context.WINDOW_SERVICE);
  windowManager.updateViewLayout(actionParent, params);
})
